<?php
function getserverinfo() {
    $server_info = [
        'cpu_cores' => null,
        'total_memory' => null,
        'used_memory' => null,
        'free_memory' => null
    ];
    if (stristr(PHP_OS, 'WIN')) {
        $output = shell_exec('wmic cpu get NumberOfCores');
        $cores = explode("\n", $output);
        $server_info['cpu_cores'] = trim($cores[1]);
    } else {
        $output = shell_exec('nproc');
        $server_info['cpu_cores'] = trim($output);
    }
    if (stristr(PHP_OS, 'WIN')) {
        $output = shell_exec('wmic memorychip get capacity');
        $memory_lines = explode("\n", $output);
        $total_memory = 0;

        foreach ($memory_lines as $line) {
            $line = trim($line);
            if (is_numeric($line)) {
                $total_memory += $line;
            }
        }

        $server_info['total_memory'] = $total_memory / (1024 * 1024 * 1024);  
    } else {
        $output = shell_exec('free -m');
        $memory_info = explode("\n", $output);
        $memory_data = explode(" ", $memory_info[1]);
        $memory_data = array_filter($memory_data);

        $server_info['total_memory'] = $memory_data[1] / 1024; 
        $server_info['used_memory'] = $memory_data[2] / 1024; 
        $server_info['free_memory'] = $memory_data[3] / 1024; 
    }

    return $server_info;
}

function calculateOptimizedDownloadSpeeds($currentUsers, $serverConfig) {
    $fileSizeSpeeds = [
        'a' => 1, 
        'b' => 5,
        'c' => 10,
        'd' => 50,
        'e' => 100,
        'f' => 500,
        'g' => 1000
    ];
    $userBandwidthDistribution = [
        '30' => 0.3, 
        '61.4' => 0.5, 
        '100' => 0.2 
    ];
    $averageUserBandwidth = 0;
    foreach ($userBandwidthDistribution as $bandwidth => $percentage) {
        $averageUserBandwidth += $bandwidth * $percentage;
    }
    $serverBandwidth = $serverConfig['bandwidth']; 
    $serverCores = $serverConfig['cores'];
    $serverMemory = $serverConfig['memory'];
    $maxUsersByCores = $serverCores / 0.05;
    $maxUsersByMemory = $serverMemory / 0.1;
    $maxSupportedUsers = min($maxUsersByCores, $maxUsersByMemory); 
    if ($currentUsers > $maxSupportedUsers) {
        $currentUsers = $maxSupportedUsers;
    }
    $vipUsers = (int)($currentUsers * 0.1);
    $nonVipUsers = $currentUsers - $vipUsers;
    $totalNonVipSpeed = 0;
    foreach ($fileSizeSpeeds as $fileSize => $speed) {
        if ($fileSize < 100) {
            $speed = 1; 
        } elseif ($fileSize >= 500) {
            $speed = min(4, $serverBandwidth / $currentUsers); 
        } else {
            $speed = 2; 
        }
        $usersInSegment = $nonVipUsers / count($fileSizeSpeeds);
        $totalNonVipSpeed += $usersInSegment * $speed;
    }
    $averageNonVipSpeed = $totalNonVipSpeed / max(1, $nonVipUsers); 
    $minNonVipSpeed = min($averageNonVipSpeed, $averageUserBandwidth);
    $nonVipBandwidth = $serverBandwidth * 0.7; 
    $maxNonVipSpeed = min($nonVipBandwidth / max(1, $nonVipUsers), $averageUserBandwidth); 
    $vipBandwidth = $serverBandwidth * 0.3; 
    $minVipSpeed = $averageUserBandwidth;
    $maxVipSpeed = min($vipBandwidth / max(1, $vipUsers), $averageUserBandwidth); 
    return [
        'non_vip' => [
            'min_speed' => $minNonVipSpeed,
            'max_speed' => $maxNonVipSpeed
        ],
        'vip' => [
            'min_speed' => $minVipSpeed,
            'max_speed' => $maxVipSpeed
        ]
    ];
}


$currentUsers = 200;
$serverConfig = [
    'bandwidth' => 80,
    'cores' => 2,       
    'memory' => 8   
];

$result = calculateOptimizedDownloadSpeeds($currentUsers, $serverConfig);
print_r($result);
